<template>
	<view class="main">
		<view class="list box box-align-center box-pack-between" @click="changePassword()">
			<u-section title="修改密码" :show-line="false" sub-title=" " style="width: 100%;"></u-section>
		</view>
		<view class="list box box-align-center box-pack-between" @click="goBind()">
			<u-section title="绑定微信" :show-line="false" sub-title=" " style="width: 100%;"></u-section>
		</view>
		<view class="list box box-align-center box-pack-between" @click="handleDestroy" v-if="role !='team'">
			<u-section title="注销账号" :show-line="false" sub-title=" "  style="width: 100%;"></u-section>
		</view>
		<view class="list box box-align-center box-pack-between" @click="update()">
			<u-section title="检查更新" :show-line="false" sub-title=" " style="width: 100%;"></u-section>
		</view>
		<view class="login-out" @click="loginOut">
			退出登录
		</view>
		<u-modal v-model="destroyShow" content="注销后所有数据不保留,是否确认?" :show-cancel-button="true" @confirm="confirm" @cancel="cancel"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: uni.getStorageSync("user"),
				destroyShow:false,
				role: uni.getStorageSync('role'),
				openid:uni.getStorageSync('openid')
			}
		},
		methods: {
			handleDestroy(){
				this.destroyShow = true
			},
			loginOut() {
				this.$api.post({url:'/sysuser/quitByOpenID',method:'post'},{openid:this.openid}).then(res=>{
					//console.log(res);
				})
				uni.removeStorage({
					key: 'user'
				});
				uni.removeStorage({
					key: 'accont'
				});
				uni.removeStorage({
					key: 'roles'
				});
				uni.removeStorage({
					key: 'role'
				});
				uni.removeStorage({
					key: 'currSchool'
				});
				uni.removeStorage({
					key: 'dclass'
				});
			
				uni.reLaunch({
					url: '/pages/index/index'
				})
				
				
			},
			goBind() {
				uni.navigateTo({
					url: '/trPageMy/bind'
				})
			},
			confirm(){
				let opts = {
					url: '/sysuser/cancel',
					method: 'post'
				};
				let param = {
					userid: this.user.id
				};
				this.$api.post(opts, param).then(res => {
					//console.log(res)
					if (res.code == 0) {
						uni.showToast({
							title:'注销成功',
							icon:"success",
							duration:1500,
							success: () => {
								this.loginOut() 	
							}
						})
					}
				})
			},
			cancel(){
				this.destroyShow = false
			},
			changePassword() {
				uni.navigateTo({
					url: '/pageHome/changePassword/changePassword'
				})
			},
			update() {
				if (wx.canIUse('getUpdateManager')) {
					const updateManager = wx.getUpdateManager()
					updateManager.onCheckForUpdate(function(res) {
						// 请求完新版本信息的回调
						if (res.hasUpdate) {
							updateManager.onUpdateReady(function() {
								wx.showModal({
									title: '更新提示',
									content: '新版本已经准备好，是否重启应用？',
									success: function(res) {
										if (res.confirm) {
											// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
											updateManager.applyUpdate()
										}
									}
								})
							})
							updateManager.onUpdateFailed(function() {
								// 新的版本下载失败
								wx.showModal({
									title: '已经有新版本了哟~',
									content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
								})
							})
						} else {
							uni.showToast({
								title:'已经是最新版本',
								icon:'none'
							})
						}
					})
				} else {
					// 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
					// wx.showModal({
					// 	title: '提示',
					// 	content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
					// 	success: () => {
					// 		 wx.exitMiniProgram({})
					// 	}
					// })
				}
			}
		}
	}
</script>

<style scoped lang="scss">
.main{
	box-sizing: border-box;
	position: relative;
	height: 100%;
	padding: 0;
	background-color: rgb(249, 249, 251);
	
	.list{
		padding: 0 40rpx;
		height: 100rpx;
		background-color: #fff;
		border-bottom: 1px solid #ccc;
		&:first-child{
			// margin-top: 40rpx;
		}
	}
	
	.login-out{
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		background-color: rgb(97, 144, 232);
		color: #fff;
		text-align: center;
	}
}
</style>
